package me.mengjiandu.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import me.mengjiandu.entity.Blog;
import me.mengjiandu.entity.Cate;
import me.mengjiandu.mapper.BlogCateMapper;
import me.mengjiandu.mapper.BlogMapper;
import me.mengjiandu.mapper.CateMapper;

@Controller
@CrossOrigin
@RequestMapping("/api")
public class MyBlogController {

	@Resource
	private CateMapper cateMapper;
	@Resource
	private BlogMapper blogMapper;
	@Resource
	private BlogCateMapper blogCateMapper;
	
	@RequestMapping("blogs")
	@ResponseBody
	protected List<Blog> blogs() {
		return blogMapper.findAll();
	}
	@RequestMapping("cates")
	@ResponseBody
	protected List<Cate> cates(){
	    return cateMapper.findAll();
	}
	@RequestMapping("add")
	@ResponseBody
	protected String blogAdd(@RequestBody Blog blog) {
		blogMapper.add(blog);
		blogCateMapper.add(blog);
		return "{res:1}";
	}
	@RequestMapping("blog")
	@ResponseBody
	protected Blog blog(String id){
		return blogMapper.findById(id);
	}
	@RequestMapping("del")
	@ResponseBody
	protected String del(String id) {
		blogCateMapper.del(id);
		blogMapper.del(id);
		return "{res:1}";
	}
}
